from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response

from review.models import Student, Classes
from review.serializers import StudentSerializer, ClassesSerializer


# Create your views here.
# class StudentViewSet(viewsets.ViewSet):
#     """
#     实现增删改查
#     """
#     def list(self, request):
#         """
#         list方法返回前端所有学生数据  需要序列化数据是一组数据也就是查询集就要传many=True
#         先获取所有学生数据
#         再给序列化器做序列化成前端数组类型
#         :param request: 前端数据
#         :return:
#         """
#         # 在模型中获取数据
#         students = Student.objects.filter(is_delete=False)
#         # 实例化序列化器对象 并且将获取到的students数据传给序列化器做数据类型转换
#         serializer = StudentSerializer(students,many=True)
#         # 返回转换后的数据的data给前端
#         return Response(serializer.data)
#
#     def create(self, request):
#         """
#         添加数据
#         :param request: 前端传入的数据
#         :return:
#         """
#         # 获取前端传入的数据
#         student = request.data
#         # 将数据反序列化转数据类型 并且要指定传参
#         serializer = StudentSerializer(data=student)
#         # 校验数据
#         serializer.is_valid(raise_exception=True)
#         """
#         持久化数据要注意
#         序列化器的初始化要传的参数为
#         def __init__(self, instance=None, data=empty, **kwargs):
#         在调用save方法时会自动调用序列化器里面的create或update方法
#         如果传了参数instance就调用update方法进行数据的修改对象数据
#         没传的话就调用create方法进行创建对象数据
#         """
#         serializer.save()
#         return Response(serializer.data)
#
#     def retrieve(self, request, pk):
#         """
#         查询的是单个的对象就不需要传many=True参数
#         :param request:
#         :param pk:
#         :return:
#         """
#         try:
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#
#         serializer = StudentSerializer(student)
#         return Response(serializer.data)
#
#
#     def update(self, request, pk=None):
#         """
#         修改学生信息
#         路由样式为 student/1/
#         :param request:
#         :param pk:
#         :return:
#         """
#         # 获取数据
#         try:
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#         # 将前端传来的数据和查找到的学生数据进行反序列化 student传给instance告诉序列化器我要做修改操作
#         serializer = StudentSerializer(student, data=request.data)
#         # 校验数据
#         serializer.is_valid(raise_exception=True)
#         # 持久化
#         serializer.save()
#
#         return Response(serializer.data)
#
#     def destroy(self, request, pk):
#         """
#         逻辑删除
#         :param request:
#         :param pk:
#         :return:
#         """
#         # 获取数据
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#         student.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(is_delete=False)
    serializer_class = StudentSerializer

class ClassesViewSet(viewsets.ModelViewSet):

    @action(methods=['get'], detail=False)
    def last(self,request):
        """
        获取最后建立的班级
        数据只有一个所以序列化不要传many=True
        :param request:
        :return:
        """
        classes = Classes.objects.last()
        serializer = ClassesSerializer(classes)
        return Response(serializer.data)

    @action(methods=['get'], detail=True, url_path=r'last_student')
    def class_last_student(self,request,pk=None):
        """
        获取指定班级的最后一个学生
        :param request:
        :return:
        """
        try:
            classes = Classes.objects.get(pk=pk)
        except Classes.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        student = classes.student_set.last()
        # student得到的是学生数据所以只能用学生序列化器进行数据的序列化
        serialzer = StudentSerializer(student).data
        return Response(serialzer)

    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassesSerializer




